package zhongbo.java.air.dictionary.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhongbo.java.air.common.vo.PageVo;
import zhongbo.java.air.dictionary.mapper.AirAlertZoneMapper;
import zhongbo.java.air.dictionary.model.AirAlertZone;
import zhongbo.java.air.dictionary.service.IAirAlertZoneService;

import java.util.List;

@Service
public class AirAlertZoneImpl implements IAirAlertZoneService<AirAlertZone> {


    @Autowired
    private AirAlertZoneMapper airAlertZoneMapper;
    @Override
    public AirAlertZone queryById(String id) {
        AirAlertZone airAlertZone = airAlertZoneMapper.selectById(id);
        if (airAlertZone == null){
            throw new RuntimeException("查询失败！当前id：" + id);
        }
        return airAlertZone;
    }

    @Override
    public PageVo<AirAlertZone> queryByPage(Integer pageNumber, Integer pageSize, String keyword, String fields
            , String oderByField, Boolean orderByAsc) {
        QueryWrapper<AirAlertZone> queryWrapper  = new QueryWrapper<>();
        //支持指定字段模糊搜索keyword
        if (StrUtil.isNotBlank(keyword) && StrUtil.isNotBlank(fields)) {
            String[] fieldsArray = fields.split(",");
            for (String field : fieldsArray) {
                queryWrapper.or().like(StrUtil.toUnderlineCase(field), keyword);
            }
        }
        //支持指定字段排序
        if (StrUtil.isNotBlank(oderByField) && orderByAsc){
            queryWrapper.orderByAsc(oderByField);
        }
        IPage<AirAlertZone> pageParam = new Page<>(pageNumber,pageSize);
        IPage<AirAlertZone> airAlertZoneIPage = airAlertZoneMapper.selectPage(pageParam, queryWrapper);
        return new PageVo<>(airAlertZoneIPage);
    }

    @Override
    public AirAlertZone insert(AirAlertZone airAircraft) {
        if (airAlertZoneMapper.exists(new QueryWrapper<AirAlertZone>().eq("zone_code", airAircraft.getZoneCode()))){
            throw new RuntimeException("区域业务编号已存在");
        }

        int insert = airAlertZoneMapper.insert(airAircraft);
        if (insert <= 0){
            throw new RuntimeException("添加失败 ->>> " + airAircraft);
        }
        return airAircraft;
    }

    @Override
    public AirAlertZone update(AirAlertZone airAircraft) {
        if (airAlertZoneMapper.exists(new QueryWrapper<AirAlertZone>().eq("zone_code", airAircraft.getZoneCode()).ne("id", airAircraft.getId()))){
            throw new RuntimeException("区域业务编号已存在");
        }

        int i = airAlertZoneMapper.updateById(airAircraft);
        if (i <= 0){
            throw new RuntimeException("修改失败 ->>> id："+ airAircraft.getId() + " 不存在； 实体信息 ->>> " + airAircraft);
        }
        return airAircraft;
    }

    @Override
    public void deleteById(String id) {
        int i = airAlertZoneMapper.deleteById(id);
        if (i <= 0){
            throw new RuntimeException("删除失败 ->>> id：" + id);
        }
    }

    @Override
    public List<AirAlertZone> queryAll() throws RuntimeException{
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.orderByAsc("create_time");
        try {
            List<AirAlertZone> airAlertZoneList = airAlertZoneMapper.selectList(queryWrapper);
            return airAlertZoneList;
        }catch (RuntimeException r){
            throw new RuntimeException("查询失败 ->>> " + r.getMessage());
        }
    }
}
